<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 *
 * @category   MVC
 * @package    Buhin_Controller
 * @author     Kazuyoshi Tlacaelel <kazu.php@gmail.com>
 */


/**
 * controller
 *
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   MVC
 * @package    Buhin_Controller
 * @author     Kazuyoshi Tlacaelel <kazu.php@gmail.com>
 */
class controller
{

    // {{{ properties

    public static

    /**
     * execution
     *
     * @var     string
     * @access  public
     */
    $execution = null,

    /**
     * template
     *
     * @var     string
     * @access  public
     */
    $template = null,

    /**
     * remodel objects
     *
     * @var     string
     * @access  public
     */
    $remodel = null,

    /**
     * model
     *
     * @var     string
     * @access  public
     */
    $model = null;

    // }}}
    // {{{ setModel

    public static function setModel($model)
    {
        self::$model = $model;
    }

    // }}}
    // {{{ setTemplate

    public static function setTemplate($template)
    {
        self::$template = $template;
    }

    // }}}
    // {{{ setExecution

    public static function setExecution($execution)
    {
        self::$execution = $execution;
    }

    // }}}
    // {{{ getModel

    public static function getModel()
    {
        return self::$model;
    }

    // }}}
    // {{{ getTemplate

    public function getTemplate()
    {
        return self::$template;
    }

    // }}}
    // {{{ getExecution

    public function getExecution()
    {
        return self::$execution;
    }

    // }}}
    // {{{ modelAndTemplate

    /**
     * modelAndTemplate
     *
     * @static
     * @access  public
     * @return  void
     */
    public static function modelAndTemplate()
    {
        return (model::exists(self::$model) && template::exists(self::$template));
    }

    // }}}
    // {{{ isLoadable

    public static function isLoadable()
    {
        $template = template::exists(self::$template);
        $model = model::exists(self::$model);
        $execution = execution::exists(self::$execution);
        return ($model || $execution || $template);
    }

    // }}}
    // {{{ load

    public static function load()
    {
        if (execution::exists(self::$execution)) {
            new self::$execution;
        }

        $model       = model::exists(self::$model);
        $no_template = (!template::exists(self::$template));

        if ($no_template) {
            self::setTemplate(config::get('mvc.template.default'));
        }

        $m = new self::$model;
        $m->execute();
        return $m;
    }

    // }}}
    // {{{ merge

    public static function merge($result, $template)
    {
        if ($result == array()) {
            return $template;
        }

        $ret = '';
        $copy = $template;
        foreach ($result as $object) {
            if (is_array($object)) {
                foreach ($object as $array) {
                    foreach ($array as $param => $value) {
                        if (self::hasParam($param, $copy)) {
                            $copy = self::replace($param, $value, $copy);
                        }
                    }
                    $ret .= $copy;
                    $copy = $template;
                }
            }
        }
        return $ret;
    }

    // }}}
    // {{{ hasParam

    public static function hasParam($param, $data)
    {
        return preg_match("/" . $param . "/", $data);
    }

    // }}}
    // {{{ replace

    /**
     * template parameter replacer
     *
     * replaces a parameter found in text by value
     *
     * @param   Mixed   $param  the parameter to replace by value
     * @param   Mixed   $value  the value to use to replace the parameter
     * @param   Mixed   $text   the text to look for replacements
     * @static
     * @access  public
     * @return  Strinhg the replaced string
     */
    public static function replace($param, $value, $text)
    {
        $param_sintax = config::get('mvc.template.param.syntax');
        // todo, verify this issue is bit buggy !
        settype($value, 'string');
        $p = sprintf($param_sintax, $param);
        return str_replace($p, $value, $text);
    }

    // }}}
    // {{{ getError

    /**
     * getError
     *
     * @static
     * @access  public
     * @return  void
     */
    public static function getError()
    {
        echo " Cannot load !";
        echo controller::getModel();
        echo template::exists(controller::getTemplate());
        echo model::exists(controller::getModel());
    }

    // }}}

}

?>
